import * as Koa from 'koa';
import { wechatService } from '../service/wechat';
import { prefix, router } from '../middleware/router';
import { response } from '../middleware/result';
import { logService } from '../service/log';

/** 微信api接口对象 */
@prefix('wechat')
class wechatController {
    /**
    * 获取签名
    * @api {get} /wechat/getAccessToken 获取微信签名AccessToken
    * @apiDescription 获取签名
    * @apiName getAccessToken
    * @apiSampleRequest /wechat/getAccessToken
    * @apiGroup wechat
    * @apiVersion 1.0.0
    */
    @router('/getAccessToken')
    async getAccessToken(ctx: Koa.Context) {
        // 记录回调日志
        logService.createlogger(ctx, 0);
        let wetoken = await wechatService.getAccessToken();
        response(ctx, wetoken)
    }
    /**
    * 获取用户唯一标识token
    * @api {post} /wechat/getOpenidByCode 获取用户唯一标识token
    * @apiDescription 获取用户唯一标识token
    * @apiName getOpenidByCode
    * @apiParam (参数) {String} code 
    * @apiSampleRequest /wechat/getOpenidByCode
    * @apiGroup wechat
    * @apiVersion 2.0.0
    */
    @router('/getOpenidByCode', { method: 'post' })
    async getOpenidByCode(ctx: Koa.Context) {
        // 记录回调日志
        logService.createlogger(ctx, 0);
        let code = ctx.request.body.code;
        let res = await wechatService.getOpenidByCode(code);
        response(ctx, res);
    }

    /**
    * 解密数据
    * @api {post} /wechat/decodeData 解密数据
    * @apiDescription 解密数据
    * @apiName decodeData
    * @apiParam (参数) {String} token 
    * @apiParam (参数) {String} encryptedData 
    * @apiParam (参数) {String} iv 
    * @apiSampleRequest /wechat/decodeData
    * @apiGroup wechat
    * @apiVersion 2.0.0
    */
    @router('/decodeData', { method: 'post' })
    async decodeData(ctx: Koa.Context) {
        // 记录回调日志
        logService.createlogger(ctx, 0);
        let token = ctx.request.body.token;
        let encryptedData = ctx.request.body.encryptedData;
        let iv = ctx.request.body.iv;
        let res = await wechatService.decodeData(token, encryptedData, iv);
        response(ctx, res);
    }
    /**
    * 支付接口
    * @api {post} /wechat/pay 支付接口
    * @apiDescription 支付接口
    * @apiName pay
    * @apiParam (参数) {String} token  用户标识token
    * @apiParam (参数) {String} mid  商户_id编号
    * @apiParam (参数) {Number} money  实际支付金额(元)
    * @apiParam (参数) {Number} winemoney  酒水消费金额(元)
    * @apiParam (参数) {Number} mealmoney  餐饮消费金额(元)
    * @apiParam (参数) {Number} conins  消费酒币(个)
    * @apiParam (参数) {Number} maxconins  本次最多可消费酒币(个)
    * @apiParam (参数) {string} paytype  支付类型 "pay": 金额>0调支付统一接口;"payok"：支付金额为0,直接执行回调；其它值计算应付酒水费用
    * @apiParam (参数) {Number} person  消费人数(个)
    * @apiParam (参数) {String} winelist  消费酒水数组(如：[{_id: "5d3023d14887963570c17881", count: 2},{_id: "5d3023d14887963570c17889", count: 1}])
    * @apiParam (参数) {Number} mealbalance  抵扣餐币
    * @apiSampleRequest /wechat/pay
    * @apiGroup wechat
    * @apiVersion 5.0.0
    */
    @router('/pay', { method: 'post' })
    async pay(ctx: Koa.Context) {
        // 记录回调日志
        logService.createlogger(ctx, 0);
        let token = ctx.request.body.token;
        let mid = ctx.request.body.mid;
        let money = ctx.request.body.money;
        let winemoney = ctx.request.body.winemoney;
        let conins = ctx.request.body.conins || 0;
        let maxconins = ctx.request.body.maxconins || 0;
        let paytype = ctx.request.body.paytype;
        let person = ctx.request.body.person || 0;
        let winelist = ctx.request.body.winelist || [];
        let mealbalance = ctx.request.body.mealbalance || 0;
        let mealmoney = ctx.request.body.mealmoney || 0;
        let res = await wechatService.pay(mid, token, money, conins, person, winelist, mealbalance, winemoney, maxconins, paytype, mealmoney, ctx);
        response(ctx, res);
    }

    /**
    * 生成订单接口
    * @api {post} /wechat/buildorder 生成订单接口
    * @apiDescription 生成订单接口
    * @apiName buildorder
    * @apiParam (参数) {String} token  用户标识token
    * @apiParam (参数) {String} mid  商户_id编号
    * @apiParam (参数) {Number} mealmoney  餐饮消费金额(元)
    * @apiParam (参数) {String} winelist  消费酒水数组(如：[{_id: "5d3023d14887963570c17881", count: 2},{_id: "5d3023d14887963570c17889", count: 1}])
    * @apiSampleRequest /wechat/buildorder
    * @apiGroup wechat
    * @apiVersion 1.0.0
    */
   @router('/buildorder', { method: 'post' })
   async buildorder(ctx: Koa.Context) {
       // 记录回调日志
       logService.createlogger(ctx, 0);
       let token = ctx.request.body.token;
       let mid = ctx.request.body.mid;
       let mealmoney = ctx.request.body.mealmoney || 0;
       let winelist = ctx.request.body.winelist || [];
       let res = await wechatService.buildorder(token, mid, mealmoney, winelist, ctx);
       response(ctx, res);
   }

    /**
    * 支付金额为0确认接口
    * @api {post} /wechat/PayOk 支付金额为0确认接口
    * @apiDescription 支付金额为0确认接口
    * @apiName PayOk
    * @apiParam (参数) {String} token  用户标识token
    * @apiParam (参数) {String} mid  商户_id编号
    * @apiParam (参数) {Number} money  支付金额(元)
    * @apiParam (参数) {Number} conins  消费酒币(个)
    * @apiParam (参数) {Number} maxconins  本次最多可消费酒币(个)
    * @apiParam (参数) {Number} person  消费人数(个)
    * @apiParam (参数) {String} winelist  消费酒水数组(如：[{_id: "5d3023d14887963570c17881", count: 2},{_id: "5d3023d14887963570c17889", count: 1}])
    * @apiParam (参数) {Number} mealbalance  抵扣餐币
    * @apiSampleRequest /wechat/PayOk
    * @apiGroup wechat
    * @apiVersion 3.0.0
    */
    // @router('/PayOk', { method: 'post' })
    // async PayOk(ctx: Koa.Context) {
    //     // 记录回调日志
    //     logService.createlogger(ctx, 0);
    //     let token = ctx.request.body.token;
    //     let mid = ctx.request.body.mid;
    //     let money = ctx.request.body.money;
    //     let conins = ctx.request.body.conins || 0;
    //     let maxconins = ctx.request.body.maxconins || 0;
    //     let person = ctx.request.body.person || 0;
    //     let winelist = ctx.request.body.winelist || [];
    //     let mealbalance = ctx.request.body.mealbalance || 0;
    //     let res = await wechatService.PayOk(mid, token, money, conins, person, winelist, mealbalance, 0, maxconins, ctx);
    //     response(ctx, res);
    // }
    /**
    * 升级会员
    * @api {post} /wechat/membership 升级会员
    * @apiDescription 升级会员
    * @apiName membership
    * @apiParam (参数) {String} token  用户标识token
    * @apiParam (参数) {String} type   会员类型，默认不传为普通会员；传值super为白金会员;传值 diamond 为钻石会员
    * @apiSampleRequest /wechat/membership
    * @apiGroup wechat
    * @apiVersion 1.0.0
    */
    @router('/membership', { method: 'post' })
    async membership(ctx: Koa.Context) {
        // 记录回调日志
        logService.createlogger(ctx, 0);
        let res = await wechatService.membership(ctx.request.body.token, ctx, ctx.request.body.type);
        response(ctx, res);
    }

    @router('/order_notice', { method: 'post' })
    async order_notice(ctx: Koa.Context) {
        let res = await wechatService.order_notice(ctx);
        response(ctx, res);
    }

    /**
    * 获取小程序二维码
    * @api {post} /wechat/buildqrcode 获取小程序二维码
    * @apiDescription 获取小程序二维码
    * @apiName buildqrcode
    * @apiParam (参数) {String} token  用户标识token
    * @apiSampleRequest /wechat/buildqrcode
    * @apiGroup wechat
    * @apiVersion 1.0.0
    */
    @router('/buildqrcode', { method: 'post' })
    async buildqrcode(ctx: Koa.Context) {
        let res = await wechatService.buildqrcode(ctx.request.body.token);
        response(ctx, res);
    }

}